package com.eascs.crm.server.manger.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;

@Repository
public abstract class BaseDao {

	@Inject
	protected JdbcSession jdbcSession;
	
	@Inject
	protected JdbcSession jdbcSessionOracle;

	public JdbcSession getJdbcSession() {
		return jdbcSession;
	}

	public void setJdbcSession(JdbcSession jdbcSession) {
		this.jdbcSession = jdbcSession;
	}

	/**
	 * 保存该表数据
	 */
	@Transactional(transactionManager = "transactionManager")
	public boolean saveEntity(Object entity) {
		return jdbcSession.insert(entity) >= 1 ? true : false;
	}

	/**
	 * 删除表数据
	 */
	public boolean delEntity(Object entity) {
		return jdbcSession.deleteByIdWithEntity(entity) >= 1 ? true : false;
	}

	/**
	 * 更新该表数据
	 */
	@Transactional(transactionManager = "transactionManager")
	public boolean updateEntity(Object entity) {
		return jdbcSession.updateById(entity) >= 1 ? true : false;
	}

	/**
	 * @param sql
	 *            执行sql语句
	 */
	@Transactional(transactionManager = "transactionManager")
	public boolean excuteSql(String sql, Object... params) {
		return jdbcSession.execute(sql, params) >= 1 ? true : false;
	}

	/**
	 * @param sql
	 *            分页查询
	 */
	public <T> PageResult<T> findByPage(SqlPageQuery<T> sqlPageQuery) {

		return this.jdbcSession.findDataWithPage(sqlPageQuery);
	}

	/**
	 * @param sql
	 *            查询符合条件的单个对象
	 * @param requiredType
	 * @param params
	 * @return
	 */
	public <T> T findEntity(String sql, Class<T> requiredType, Object... params) {

		return jdbcSession.findEntity(sql, requiredType, params);
	}

	/**
	 * @Title: findEntitys
	 * @Description: 查询符合条件的结果集
	 * @author 张庆奇
	 * @param sql
	 * @param requiredType
	 * @param params
	 * @return
	 * @return List<T>
	 * @throws @date
	 *             2016年5月27日 上午11:07:01
	 */
	public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {

		return jdbcSession.findEntitys(sql, requiredType, params);
	}
	
	/**
	 * @Title: getById
	 * @Description: TODO(通过id得到实体对象)
	 * @author lwx
	 * @param entityClass
	 * @param id
	 * @return
	 * @return T
	 * @throws
	 * @date 2016年5月27日 下午4:53:19
	 */
	public <T> T getById(Class<T> entityClass, Object... id) {
		return jdbcSession.getById(entityClass, id);
	}
	
	public List<Object[]> findObjectsList(String sql, Object... params) {
		return jdbcSession.findObjectsList(sql, params);
	}
	
	
	public String  findString(String sql, Object... params){
		return jdbcSession.findString(sql,params);
	}
	
	public boolean saveEntityOracle(Object entity) {
		return jdbcSessionOracle.insert(entity) >= 1 ? true : false;
	}
	
	public boolean excuteSqlOrecle(String sql, Object... params) {
		return jdbcSessionOracle.execute(sql, params) >= 1 ? true : false;
	}
	
	public boolean excute(String sql, Object... params) {
		return jdbcSession.execute(sql, params) >= 1 ? true : false;
	}
	
	public int findIntegerOrecle(String Sql){
		return jdbcSessionOracle.findInteger(Sql);
	}
	
	public int findInteger(String Sql, Object... params){
		return jdbcSession.findInteger(Sql,params);
	}
	
	public List<Object[]> findList(String sql, Object... params) {
		return jdbcSession.findObjectsList(sql, params);
	}

}

